package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySQLRaavareBatchDAO implements RaavareBatchDAO {

	private Connector conn = null;
	private ResultSet rs = null;

	public MySQLRaavareBatchDAO() throws DALException {
		conn = new Connector();
	}

	public RaavareBatchDTO getRaavareBatch(int rbId) throws DALException {
		RaavareBatchDTO raavarebatch = null;
		String query = "SELECT * FROM raavarebatch WHERE rbId = " + rbId + ";";
		rs = conn.doQuery(query);
		try {
			if (!rs.first()) {
				throw new DALException("R�vare batch " + rbId + "findes ikke");

			}
			raavarebatch = new RaavareBatchDTO(rs.getInt(1), rs.getInt(2),
					rs.getDouble(3), rs.getString(4));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return raavarebatch;

	}

	public ArrayList<RaavareBatchDTO> getRaavareBatchList() throws DALException {
		ArrayList<RaavareBatchDTO> rawBatchList = new ArrayList<RaavareBatchDTO>();
		String query = "SELECT * FROM raavarebatch";
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				rawBatchList.add(new RaavareBatchDTO(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getString(4)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rawBatchList;
	}

	public List<RaavareBatchDTO> getRaavareBatchList(int raavareId) throws DALException {
		List<RaavareBatchDTO> rbDTO = new ArrayList<RaavareBatchDTO>();
		String query = "SELECT * FROM raavarebatch WHERE raavareId = " + raavareId;
		rs = conn.doQuery(query);
		try {
			if (!rs.first()) {
				throw new DALException("R�vare batch med r�vareId " + raavareId + "findes ikke");
			}
			rbDTO.add(new RaavareBatchDTO(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getString(4)));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rbDTO;
	}

	public List<RaavareBatchDTO> getRaavareBatchList(int first, int count) throws DALException {
		List<RaavareBatchDTO> raavareBatchList = new ArrayList<RaavareBatchDTO>();
		String query = "SELECT * FROM raavarebatch LIMIT " + first + "," + count;
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				raavareBatchList.add(new RaavareBatchDTO(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getString(4)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return raavareBatchList;
	}

	public int getCount() throws DALException {
		rs = conn.doQuery("SELECT COUNT(rbId) FROM raavarebatch");
		try {
			if (rs.next())
				return rs.getInt(1);
			else
				return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public void createRaavareBatch(RaavareBatchDTO raavarebatch) throws DALException {
		System.err.println("RbId: " + raavarebatch.getRbId());
		System.err.println("RaavareId: " + raavarebatch.getRaavareId());
		System.err.println("Maengde: " + raavarebatch.getMaengde());
		System.err.println("UdloebsDato: " + raavarebatch.getUdloebsDato());
		conn.doUpdate("INSERT INTO raavarebatch(rbId, raavareId, maengde,udloebsdato)VALUES"
				+ "("
				+ raavarebatch.getRbId() + ", "
				+ raavarebatch.getRaavareId() + ", "
				+ raavarebatch.getMaengde() + ", "
				+ raavarebatch.getUdloebsDato() + "')");
	}

	public void updateRaavareBatch(RaavareBatchDTO raavarebatch) throws DALException {
		conn.doUpdate("UPDATE raavarebatch SET rbId = "
				+ raavarebatch.getRbId() + ",raavareId = "
				+ raavarebatch.getRaavareId() + ", maengde = "
				+ raavarebatch.getMaengde() + ", udloebsdato = '"
				+ raavarebatch.getUdloebsDato() + "' WHERE rbId = "
				+ raavarebatch.getRbId() + " AND raavareId = "
				+ raavarebatch.getRaavareId());
	}
}
